package com.kx.smsplantform.comsume;

import com.kx.smsplantform.execute.StrategyExecutor;
import com.kx.smsplatform.common.constants.RabbitMQConstants;
import com.kx.smsplatform.common.model.StandardSubmit;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 消费者只需要获取队列中的消息，交给Executor执行策略链即可。
 *
 * @Author Sun
 * @Date 2021/7/2 11:24
 */
@Slf4j
@Component
public class PreSendListener {

    /**
     * 使用策略执行器来执行需要执行的某个策略
     */
    @Autowired
    private StrategyExecutor executor;

    //指定需要监听的队列
    @RabbitListener(queues = RabbitMQConstants.TOPIC_PRE_SEND)
    /**
     * @param submit   需要消费的对象
     * @param channel  连接交换机的通道
     * @param message
     **/
    public void consume(StandardSubmit submit, Channel channel, Message message) throws IOException {
        log.info("【策略模块-PreSendListener】 接收到{}队列的的消息， submit = {}",RabbitMQConstants.TOPIC_PRE_SEND,submit);
        //1. 调用executor执行策略链
        executor.executor(submit);
        //2.手动ACK
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("【策略模块-PreSendListener】 消费完毕 submit = {}",submit);
    }
}
